package com.pengbei.enshrine.common.utils;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.pengbei.enshrine.R;

public class GlideUtils {

    /**
     * 加载头像
     */
    public static void LoadHead(Activity activity, ImageView imageView, String url) {
        if (activity.isDestroyed()) {
            return; // 如果 Activity 已销毁，直接返回
        }
        Glide.with(activity).load(url)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
//                .placeholder(R.mipmap.ic_person_grey)
//                .error(R.mipmap.ic_person_grey)
                .skipMemoryCache(false)
                .into(imageView);
    }

    /**
     * 加载头像
     */
    public static void LoadHeadObject(Context context, ImageView imageView, Object url) {
        Glide.with(context)
                .load(url)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
//                .placeholder(R.drawable.ic_person_grey)
//                .error(R.drawable.ic_person_grey)
                .skipMemoryCache(false)
                .into(imageView);
    }

    /**
     * 加载图片
     */
    public static void LoadImage(Activity activity, ImageView imageView, String url) {
        if (activity.isDestroyed()) {
            return; // 如果 Activity 已销毁，直接返回
        }
        Glide.with(activity)
                .load(url)
                .centerCrop()
//                .placeholder(R.mipmap.ic_image)
//                .error(R.mipmap.ic_image)
                .skipMemoryCache(false)
                .into(imageView);
    }

    /**
     * 加载圆角图片
     * */
    public static void LoadRadiusImage(Activity activity, ImageView imageView, String url, int radius) {
        if (activity.isDestroyed()) {
            return; // 如果 Activity 已销毁，直接返回
        }
        RequestOptions options = new RequestOptions()
                .transform(new CenterCrop(), new RoundedCorners(dip2px(activity,radius)));
        Glide.with(activity).load(url)
                .apply(options)
//                .placeholder(R.mipmap.img_loading)
//                .error(R.mipmap.img_loading)
                .priority(Priority.HIGH)
                .skipMemoryCache(false)
                .into(imageView);
    }


    /**
     * 加载图片 四个角可设置各自的角度
     * */
    public static void LoadRadiusImage(Activity activity, ImageView imageView, String url, float radius, boolean leftTop, boolean rightTop, boolean leftBottom, boolean rightBottom) {
        if (activity.isDestroyed()) {
            return; // 如果 Activity 已销毁，直接返回
        }
        FilletTransformation transform = new FilletTransformation(dip2px(activity, radius), leftTop, rightTop, leftBottom, rightBottom);
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .transform(transform);
        Glide.with(activity).load(url)
                .apply(options)
//                .placeholder(R.mipmap.img_loading)
//                .error(R.mipmap.img_loading)
                .priority(Priority.HIGH)
                .skipMemoryCache(false)
                .into(imageView);
    }

    /**
     * dip转px
     *
     * @param context
     * @param dipValue
     * @return
     */
    public static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }


}
